<!--
 * @file 轨迹查询面板组件
 * @description 用于历史轨迹查询和日期选择功能
 * @date 2024-04-01
 -->
<template>
  <view class="track-query-panel">
    <!-- 日期选择器区域 -->
    <view class="date-picker-box">
      <picker 
        mode="date" 
        :value="selectedDate"
        start="2024-01-01"
        :end="TODAY"
        @change="handleDateChange"
      >
        <view class="date-picker">
          <text class="iconfont icon-calendar"></text>
          <text class="date-text">{{ selectedDate }}</text>
          <text class="iconfont icon-down"></text>
        </view>
      </picker>
    </view>
  </view>
</template>

<script lang="ts">
// 将formatDate函数定义在<script setup>外部
function formatDate(date: Date): string {
  return date.toISOString().split('T')[0];
}
// 预计算当前日期
const TODAY = formatDate(new Date());
</script>

<script setup lang="ts">
// @ts-ignore
import { ref, watch } from 'vue';

// 定义组件属性
interface Props {
  date: string;
}

const props = withDefaults(defineProps<Props>(), {
  date: TODAY // 使用预计算的值
});

// 定义事件
const emit = defineEmits([
  'update:date',
  'date-change'
]);

// 内部状态
const selectedDate = ref(props.date);

// 监听props变化
watch(() => props.date, (newVal: string) => {
  selectedDate.value = newVal;
});

// 方法
interface DateChangeEvent {
  detail: {
    value: string;
  }
}

function handleDateChange(e: DateChangeEvent): void {
  selectedDate.value = e.detail.value;
  emit('update:date', selectedDate.value);
  emit('date-change', selectedDate.value);
}
</script>

<style lang="scss" scoped>
.track-query-panel {
  padding: 12px;
  
  .date-picker-box {
    width: 100%;
    
    .date-picker {
      height: 36px;
      background-color: #fff;
      border: 1px solid #e8e8e8;
      border-radius: 4px;
      padding: 0 12px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      font-size: 14px;
      color: #333;
      
      .iconfont {
        font-size: 14px;
        color: #999;
        
        &.icon-calendar {
          margin-right: 4px;
        }
        
        &.icon-down {
          font-size: 12px;
        }
      }
      
      .date-text {
        flex: 1;
        margin-left: 8px;
      }
    }
  }
}
</style> 